/*
Copyright (c) 2007,2007 Elizeu Nogueira da R. Jr.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.
    * Neither the name of the Elizeu Nogueira da R. Jr. nor the names of its 
      contributors may be used to endorse or promote products derived from this 
      software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


This package is based on HypersonicSQL, originally developed by Thomas Mueller.
and HSQLDB original developed by the HSQL Development Group
*/

package br.com.dreamsource.mobile.jmesql;

class TriggerDef extends Thread {

    /**
     * member variables
     */
    static final int NUM_TRIGGER_OPS = 3;    // ie ins,del,upd
    static final int NUM_TRIGS = NUM_TRIGGER_OPS * 2 * 2;

    // indexes into the triggers Vector
    static final int  INSERT_AFTER = 0;
    static final int  DELETE_AFTER = 1;
    static final int  UPDATE_AFTER = 2;
    static final int  INSERT_BEFORE = INSERT_AFTER + NUM_TRIGGER_OPS;
    static final int  DELETE_BEFORE = DELETE_AFTER + NUM_TRIGGER_OPS;
    static final int  UPDATE_BEFORE = UPDATE_AFTER + NUM_TRIGGER_OPS;
    static final int  INSERT_AFTER_ROW = INSERT_AFTER + 2 * NUM_TRIGGER_OPS;
    static final int  DELETE_AFTER_ROW = DELETE_AFTER + 2 * NUM_TRIGGER_OPS;
    static final int  UPDATE_AFTER_ROW = UPDATE_AFTER + 2 * NUM_TRIGGER_OPS;
    static final int  INSERT_BEFORE_ROW = INSERT_BEFORE + 2 * NUM_TRIGGER_OPS;
    static final int  DELETE_BEFORE_ROW = DELETE_BEFORE + 2 * NUM_TRIGGER_OPS;
    static final int  UPDATE_BEFORE_ROW = UPDATE_BEFORE + 2 * NUM_TRIGGER_OPS;
    String	      name;
    String	      when;
    String	      operation;
    boolean	      forEachRow;
    Table	      table;
    Trigger	      trig;
    String	      fire;
    int		      vectorIndx;    // index into Vector[]
    Object	      rowForTrigger[];    // for firing thread to fire to pass to JSP
    protected boolean busy;	     // firing trigger in progress
    protected boolean valid;    // parsing valid

    public TriggerDef(String sName, String sWhen, String sOper,
		      boolean bForEach, Table pTab, Trigger pTrig,
		      String sFire) {
	name = sName.toUpperCase();
	when = sWhen.toUpperCase();
	operation = sOper.toUpperCase();
	forEachRow = bForEach;
	table = pTab;
	trig = pTrig;
	fire = sFire;
	vectorIndx = SqlToIndex();
	busy = false;

	if (vectorIndx < 0) {
	    valid = false;
	} else {
	    valid = true;
	}
    }

    public StringBuffer toBuf() {
	StringBuffer a = new StringBuffer("CREATE TRIGGER ");

	a.append(name);
	a.append(" ");
	a.append(when);
	a.append(" ");
	a.append(operation);
	a.append(" ON ");
	a.append(table.getName());

	if (forEachRow) {
	    a.append(" FOR EACH ROW ");
	}

	a.append(" CALL ");
	a.append(fire);

	return a;
    }

    public int SqlToIndex() {
	int indx;

	if (operation.equals("INSERT")) {
	    indx = INSERT_AFTER;
	} else if (operation.equals("DELETE")) {
	    indx = DELETE_AFTER;
	} else if (operation.equals("UPDATE")) {
	    indx = UPDATE_AFTER;
	} else {
	    indx = -1;
	}

	if (when.equals("BEFORE")) {
	    indx += NUM_TRIGGER_OPS;    // number of operations
	} else if (!when.equals("AFTER")) {
	    indx = -1;
	}

	if (forEachRow) {
	    indx += 2 * NUM_TRIGGER_OPS;
	}

	return indx;
    }

    public void run() {
	boolean keepGoing = true;

	while (keepGoing) {
	    pop();
	}
    }

    public synchronized void pop() {
	if (!busy) {
	    try {
		wait();    // this releases the lock monitor
	    } catch (InterruptedException e) { /* ignore and resume */
	    }
	}

	trig.fire(name, table.getName(), rowForTrigger);

	busy = false;

	notify();    // notify push's wait
    }

    public synchronized void push(Object row[]) {
	if (busy) {
	    try {
		wait();
	    } catch (InterruptedException e) { /* ignore and resume */
	    }
	}

	busy = true;
	rowForTrigger = row;

	notify();    // notify pop's wait
    }

    static public int numTrigs() {
	return NUM_TRIGS;
    }

    public boolean isBusy() {
	return busy;
    }

    public boolean isValid() {
	return valid;
    }

}
